package game.scene
{
	import away3d.containers.*;
	
	import engine.core.*;
	import engine.graphics.away3d.*;
	import engine.loaders.*;
	import engine.math.*;
	import engine.sound.*;
	
	import game.*;
	import game.actor.*;
	import game.core.*;
	import game.graphics.away3d.*;
	
	public class RxGameCamera extends RxEntity
	{
		private var m_view:View3D;
		private var m_state:String;
		private var m_unscaledOrthoWidth:Number;
		private var m_unscaledOrthoHeight:Number;
		private var m_cameraWorldPos:RxVector4;
		private var m_targetWorldPos:RxVector4;
		private var m_targetViewPos:RxVector4;
		private var m_actorWorldPos:RxVector4;
		private var m_actorViewPos:RxVector4;
		private var m_heading:Number;
		private var m_pitch:Number;
		private var m_lastAspect:Number;
		private var m_shakeSize:Number;
		private var m_shakeTime:Number;
		private var m_boundaryClipPlanes:Vector.<RxPlane>;
		private var m_targetedActor:RxActor;
		private static var s_instance:RxGameCamera;
		
		public function RxGameCamera(view:View3D, name:String)
		{
			super(RxEntityPhase.ENTITY_PHASE_CAMERA, RxEntityType.ENTITY_TYPE_CAMERA, name);
			s_instance = this;
			this.m_view = view;
			this.m_cameraWorldPos = new RxVector4(0, 0, 0, 1);
			this.m_targetWorldPos = null;
			this.m_targetViewPos = null;
			this.m_actorWorldPos = null;
			this.m_actorViewPos = null;
			this.ResetView();
			this.m_boundaryClipPlanes = null;
			this.m_state = "follow";
		}
		
		private function ResetView() : void
		{
			this.m_cameraWorldPos.Set(0, 0, 0, 1);
			this.m_pitch = -30 * (Math.PI / 180);
			this.m_heading = 45 * (Math.PI / 180);
			var camera:* = RxCamera3D(this.m_view.camera);
			camera.moveTo(this.m_cameraWorldPos.x, this.m_cameraWorldPos.y, this.m_cameraWorldPos.z);
			camera.rotateTo(this.m_pitch, this.m_heading, 0);
			var len:* = RxOrthographicLens(camera.lens);
			this.m_unscaledOrthoWidth = 1200;
			this.m_unscaledOrthoHeight = 1200;
			len.SetOrthoWidth(this.m_unscaledOrthoWidth);
			len.SetOrthoHeight(this.m_unscaledOrthoHeight);
			len.near = 1;
			len.far = 10000;
			this.CreateBoundaryClipPlanes(this.m_cameraWorldPos, this.m_pitch, this.m_heading, 0, len.GetOrthoWidth(), len.GetOrthoHeight(), len.near, len.far);
		}
		
		public function CreateBoundaryClipPlanes(cameraWorldPos:RxVector4, pitch:Number, heading:Number, alpha:Number, width:Number, height:Number, near:Number, far:Number) : void
		{
			if (this.m_boundaryClipPlanes == null)
			{
				this.m_boundaryClipPlanes = new Vector.<RxPlane>(6, true);
				this.m_boundaryClipPlanes[0] = new RxPlane(0, 0, 0, 0);
				this.m_boundaryClipPlanes[1] = new RxPlane(0, 0, 0, 0);
				this.m_boundaryClipPlanes[2] = new RxPlane(0, 0, 0, 0);
				this.m_boundaryClipPlanes[3] = new RxPlane(0, 0, 0, 0);
				this.m_boundaryClipPlanes[4] = new RxPlane(0, 0, 0, 0);
				this.m_boundaryClipPlanes[5] = new RxPlane(0, 0, 0, 0);
			}
			var _loc_9:* = RxMatrix4x4.PoolAlloc();
			var _loc_10:* = RxMatrix4x4.PoolAlloc();
			var _loc_11:* = RxMatrix4x4.PoolAlloc();
			RxCamera3D.CreateOrthoViewProjectionMatrix(_loc_9, _loc_10, cameraWorldPos, pitch, heading, alpha, width, height, near, far);
			RxMatrix4x4.Mul4x4(_loc_11, _loc_9, _loc_10);
			RxCamera3D.CreateFrustumPlanes(this.m_boundaryClipPlanes, _loc_11);
			RxMatrix4x4.PoolFree(_loc_9);
			RxMatrix4x4.PoolFree(_loc_10);
			RxMatrix4x4.PoolFree(_loc_11);
		}
		
		public function GetPosition() : RxVector4
		{
			return this.m_cameraWorldPos;
		}
		
		public static function get instance() : RxGameCamera
		{
			return s_instance;
		}
		
		public function GetTarget() : RxActor
		{
			return this.m_targetedActor;
		}
		
		public function SetTarget(param1:RxActor) : void
		{
			this.m_targetedActor = param1;
			this.m_targetWorldPos = null;
		}
		
		public function ParseFromXML(param1:XMLList) : Boolean
		{
			if (param1 == null || param1.length() == 0)
			{
				return false;
			}
			var _loc_2:* = RxCamera3D(this.m_view.camera);
			var _loc_3:* = RxOrthographicLens(_loc_2.lens);
			this.m_cameraWorldPos.x = Number(param1.@posX);
			this.m_cameraWorldPos.y = Number(param1.@posY);
			this.m_cameraWorldPos.z = Number(param1.@posZ);
			this.m_pitch = Number(param1.@rotX);
			this.m_heading = Number(param1.@rotY);
			this.m_pitch = this.m_pitch * (Math.PI / 180);
			this.m_heading = this.m_heading * (Math.PI / 180);
			this.m_unscaledOrthoWidth = Number(param1.@viewWidth);
			this.m_unscaledOrthoHeight = Number(param1.@viewHeight);
			var _loc_4:* = RxClientVars.GetInt("videoCaptureSetup");
			var _loc_5:* = RxClientVars.GetNumber("videoCaptureOrtho");
			if (!_loc_4)
			{
				_loc_3.SetOrthoWidth(this.m_unscaledOrthoWidth);
				_loc_3.SetOrthoHeight(this.m_unscaledOrthoHeight);
			}
			else
			{
				_loc_3.SetOrthoWidth(_loc_5);
				_loc_3.SetOrthoHeight(_loc_5);
			}
			var _loc_6:* = Number(param1.@projWidth);
			var _loc_7:* = Number(param1.@projHeight);
			var _loc_8:* = Number(param1.@minY);
			var _loc_9:* = Number(param1.@maxY);
			_loc_3.near = Number(param1.@nearClip);
			_loc_3.far = Number(param1.@farClip);
			_loc_2.moveTo(this.m_cameraWorldPos.x, this.m_cameraWorldPos.y, this.m_cameraWorldPos.z);
			_loc_2.rotateTo(this.m_pitch, this.m_heading, 0);
			_loc_2.Update();
			var _loc_10:* = RxRenderer(this.m_view.renderer);
			RxRenderer(this.m_view.renderer).SetUVProjection(this.m_cameraWorldPos, this.m_pitch, this.m_heading, 0, _loc_6, _loc_7, _loc_3.near, _loc_3.far);
			var _loc_11:Number = -2000;
			_loc_10.CalculateWorldXZBounds(_loc_11);
			var _loc_12:* = _loc_10.GetMinViewAlignedPosition();
			var _loc_13:* = _loc_10.GetMaxViewAlignedPosition();
			var _loc_14:* = _loc_12.x;
			var _loc_15:* = _loc_13.x;
			var _loc_16:* = _loc_12.y;
			var _loc_17:* = _loc_13.y;
			var _loc_18:* = _loc_15 - _loc_14 - _loc_3.GetOrthoWidth() * _loc_3.GetAspectRatio();
			var _loc_19:* = _loc_17 - _loc_16 - _loc_3.GetOrthoHeight();
			var _loc_20:* = 0.5 * (_loc_15 + _loc_14);
			var _loc_21:* = 0.5 * (_loc_17 + _loc_16);
			var _loc_22:* = RxVector4.PoolAlloc();
			var _loc_23:* = RxVector4.PoolAlloc();
			var _loc_24:* = RxVector4.PoolAlloc();
			_loc_22.Copy(_loc_2.GetXAxis());
			_loc_23.Copy(_loc_2.GetYAxis());
			RxVector4.MulScalar(_loc_22, _loc_22, _loc_20);
			RxVector4.MulScalar(_loc_23, _loc_23, _loc_21);
			RxVector4.Add(_loc_24, this.m_cameraWorldPos, _loc_22);
			RxVector4.Add(_loc_24, _loc_24, _loc_23);
			this.CreateBoundaryClipPlanes(_loc_24, this.m_pitch, this.m_heading, 0, _loc_18, _loc_19, _loc_3.near, _loc_3.far);
			RxVector4.PoolFree(_loc_22);
			_loc_22 = null;
			RxVector4.PoolFree(_loc_23);
			_loc_23 = null;
			RxVector4.PoolFree(_loc_24);
			_loc_24 = null;
			return true;
		}
	}
}